﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC4_Test.Models;

namespace MVC4_Test.Controllers
{
    public class QuestionController : Controller
    {
        private StackOverflowContext db = new StackOverflowContext();

        public ActionResult Show(int id = 0)
        {
            return View(db.Questions.Find(id));
        }
        
        // werkt hetzelfde als favorite en question andere controller
        // GET: /Question/
        public ActionResult Index()
        {
            ViewBag.Counter = db.Questions.Count();

            return View(db.Questions.Include("QuestionTag").Include("Answer").ToList());
        }
        

        // zoek functie waarbij een vraag wordt opgeroepen gebaseerd op welke tag de vraag heeft ook met ingebouwd tab query systeem
        // Get /question/tagged/1
        public ActionResult Tagged(int id = 0, string tab = "featured")
        {
            var result = db.QuestionTags.Where(p => p.TagId == id).Select(p => p.Question);

            switch (tab)
            {
                case "featured":
                    result = db.QuestionTags.Where(p => p.TagId == id).OrderBy(p => p.Question.Date).Select(p => p.Question);
                    break;
                /*case "orderby":
                    result = db.QuestionTags.Where(p => p.TagId == id).OrderByDescending(p => p.Question.Date).Select(p => p.Question);
                    break;*/
                case "hot":
                    result = db.QuestionTags.Where(p => p.TagId == id).OrderByDescending(p => p.Question.View).Select(p => p.Question).Distinct();
                    break;
                case "week":
                    result = db.QuestionTags.Where(p => p.TagId == id).OrderByDescending(p => p.Question.Date.Day * 7).Select(p => p.Question).Distinct();
                    break;
                case "month":
                    result = db.QuestionTags.Where(p => p.TagId == id).OrderByDescending(p => p.Question.Date.Month).Select(p => p.Question).Distinct();
                    break;
                default:
                    result = db.QuestionTags.Where(p => p.TagId == id).Select(p => p.Question);
                    break;

            }

            return View(result);
        }


        // query systee
        // Get /questions/1
        public ActionResult Questions(int id = 0, string tab = "hot")
        {
            var result = db.Questions.Include("QuestionTag").ToList();

            
                switch (tab)
            {
                case "frequent":
                    result = db.Questions.Include("QuestionTag").Include("Answer").Include("Answer").ToList();
                    break;
                case "orderby":
                    result = db.Questions.Include("QuestionTag").OrderByDescending(p => p.Date).ToList();
                    break;
                case "hot":
                    result = db.Questions.Include("QuestionTag").OrderByDescending(p => p.View).ToList();
                    break;
                case "week":
                    result = db.Questions.Include("QuestionTag").OrderByDescending(p => p.Date.Day * 7).ToList();
                    break;
                //case "unanswerd":
                //    result = db.Questions.Include("QuestionTag").OrderByDescending(p => p.Date.Month).Where(Answer = 0).ToList();
                //    break;
                default:
                    result = db.Questions.Include("QuestionTag").ToList();
                    break;

            }

            return View(result);
        }


        //
        // GET: /Question/Details/5

        public ActionResult Details(int id = 0)
        {
            Question question = db.Questions.Find(id);
            if (question == null)
            {
                return HttpNotFound();
            }
            return View(question);
        }

        //
        // GET: /Question/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Question/Create

        [HttpPost]
        public ActionResult Create(Question question)
        {
            if (ModelState.IsValid)
            {
                db.Questions.Add(question);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(question);
        }

        //
        // GET: /Question/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Question question = db.Questions.Find(id);
            if (question == null)
            {
                return HttpNotFound();
            }
            return View(question);
        }

        //
        // POST: /Question/Edit/5

        [HttpPost]
        public ActionResult Edit(Question question)
        {
            if (ModelState.IsValid)
            {
                db.Entry(question).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(question);
        }

        //
        // GET: /Question/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Question question = db.Questions.Find(id);
            if (question == null)
            {
                return HttpNotFound();
            }
            return View(question);
        }

        //
        // POST: /Question/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Question question = db.Questions.Find(id);
            db.Questions.Remove(question);
            db.SaveChanges();
            return RedirectToAction("Index");
        }


        // search functie die kijk naar een bepaald id haalt het uit een textbox
        // GET: /Question/Search/id
        public ActionResult Search(string q)
        {

            ViewBag.Query = q;

            var result = db.Questions.Include("Answer").Include("QuestionTag").Where(p => p.Text.Contains(q) || p.Title.Contains(q));
            return View(result);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        //
        // GET: /Question/Ask
        [HttpGet]
        [Authorize]
        public ActionResult Ask()
        {
            return View();
        }

        // zorgt ervor dat je kan asken zet savet meteen de datetime uit je computer
        // GET: /Question/Ask
        [HttpPost]
        [Authorize]
        [ValidateInput(false)]
        public ActionResult Ask(Question question)
        {
            if (ModelState.IsValid)
            {
                question.Date = DateTime.Now;
                question.View = 0;
                db.Questions.Add(question);
                db.SaveChanges();
                return RedirectToAction("index");
            }

            return View(question);
        }

    }
}